home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Talking Telnet / source / Speech / SpeakSelection.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  2.2 KB  |  115 lines  |  [TEXT/CWIE]

  1. #include    "speech.proto.h"
  2. #include    "maclook.proto.h"
  3. #include    "rsdefs.h"
  4. #include    "rsinterf.proto.h"
  5. #include    <Memory.h>
  6. #include    <Speech.h>
  7. #include    <Windows.h>
  8.  
  9.  
  10. SpeechChannel        gSpeechChannel = nil;
  11. Handle                gSpeechText = nil;
  12.  
  13.  
  14. static pascal void MySpeechDoneCallback(SpeechChannel channel, Boolean * doneSpeaking)
  15. {
  16.     *doneSpeaking = true;
  17. }
  18.  
  19.  
  20.  
  21. void SpeakSelection(WindowPtr window)
  22. {
  23.     OSErr    err;
  24.     
  25.     do {
  26.         short    w;
  27.         Handle    speechText;
  28.         Size    numberSpeechTextBytes;
  29.         
  30.         if (!gCanSpeak || nil == window) {
  31.             break;
  32.         }
  33.         
  34.         /* convert window to a screen index */
  35.         
  36.         w = RSfindvwind(window);
  37.         if (0 > w) {
  38.             break;
  39.         }
  40.         
  41.         /* get the selected text */
  42.         
  43.         speechText = RSGetTextSel(w, 0);
  44.         if (nil == speechText) {
  45.             break;
  46.         }
  47.         
  48.         /* Stop the old speech channel */
  49.         
  50.         StopSpeaking();
  51.         
  52.         /* Get a speech channel */
  53.         
  54.         gSpeakingVoiceIndex = gSelectedVoiceIndex;
  55.         MoveHHi((Handle)gVoices);
  56.         HLock((Handle)gVoices);
  57.         err = NewSpeechChannel(&(*gVoices)[gSpeakingVoiceIndex], &gSpeechChannel);
  58.         HUnlock((Handle)gVoices);
  59.         if (noErr != err) {
  60.         
  61.             /* The voice that the user selected doesn't work; let's try the rest */
  62.             
  63.             HLock((Handle)gVoices);
  64.             for (gSpeakingVoiceIndex = 0; gSpeakingVoiceIndex < gNumberVoices; gSpeakingVoiceIndex++) {
  65.                 err = NewSpeechChannel(&(*gVoices)[gSpeakingVoiceIndex], &gSpeechChannel);
  66.                 if (noErr == err) {
  67.                     break;
  68.                 }
  69.             }
  70.             HUnlock((Handle)gVoices);
  71.             if (gSpeakingVoiceIndex == gNumberVoices) {
  72.                 StopSpeaking();
  73.                 break;
  74.             }
  75.         }
  76.         
  77.         /* Set up the callback */
  78.         
  79.         err = SetSpeechInfo(gSpeechChannel, soRefCon, &gDoneSpeaking);
  80.         if (noErr != err) {
  81.             StopSpeaking();
  82.             break;
  83.         }
  84.         
  85.         err = SetSpeechInfo(gSpeechChannel, soSpeechDoneCallBack, MySpeechDoneCallback);
  86.         if (noErr != err) {
  87.             StopSpeaking();
  88.             break;
  89.         }
  90.         
  91.         /* Start speaking the new text */
  92.         
  93.         gSpeechText = speechText;
  94.         MoveHHi(gSpeechText);
  95.         HLock(gSpeechText);
  96.         numberSpeechTextBytes = GetHandleSize(gSpeechText);
  97.         
  98.         err = SpeakText(gSpeechChannel, *gSpeechText, numberSpeechTextBytes);
  99.         if (noErr != err) {
  100.             StopSpeaking();
  101.             break;
  102.         }
  103.         
  104.         /* Flash the selection, and fix the menu */
  105.         
  106.         FlashSelection(w);
  107.         AdjustSpeechMenu(true);
  108.         
  109.     } while (false);
  110.     
  111.     if (noErr != err) {
  112.         DoSpeechError(err);
  113.     }
  114. }
  115.